﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using Newtonsoft.Json.Linq;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers.UserManagement
{
    public class User_DistrictController : Controller
    {
        //
        // GET: /User_District/
        ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取全部用户区域地址
        /// </summary>
        /// <returns></returns>
        public ActionResult getgrids()
        {
            try
            {
                string result = service.Get_User_Districts();
                return Content(result);
            }
            catch (Exception ex)
            {
                return Content("操作异常：" + ex.Message + "请联系管理员解决");
            }
        }

        /// <summary>
        /// 获取区域地址信息
        /// </summary>
        /// <param name="DistrictID">区域ID</param>
        /// <returns></returns>
        public ActionResult User_Districts_Details(string DistrictID)
        {
            try
            {
                string result = service.Get_User_DistrictsByDistrictID(DistrictID, "", 1);
                return Content(result);
            }
            catch (Exception ex)
            {
                return Content("操作异常：" + ex.Message + "请联系管理员解决");
            }
        }

        /// <summary>
        /// 增、删、改用户区域地址
        /// </summary>
        /// <param name="Tag">操作标签（1：新增；2：修改；3：删除）</param>
        /// <param name="User_District"></param>
        /// <returns></returns>
        public ActionResult UpdateUser_District(int Tag, User_District_T User_District)
        {
            try
            {
                string result = "";
                View_OperUser user = (View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }
                #region 转换传入的信息
                User_District_T New_District = new User_District_T
                {
                    DistrictID = User_District.DistrictID,
                    DistrictName = User_District.DistrictName,
                    DelFormMon = User_District.DelFormMon,
                    DistrictState = User_District.DistrictState,
                };
                #endregion
                string a = New_District.ToJson();
                result = service.SetUser_District(user.OperID, New_District.ToJson(), Tag, GetYZM._YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();
                if (bs == "1")
                {
                    return Content(bs);
                }
                else
                {
                    return Content(searcho["Msg"].ToString());
                }
            }
            catch (Exception ex)
            {
                return Content("操作异常：" + ex + "请联系管理员解决");
            }
        }
    }
}
